using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IrrlichtNETCP;
using IrrlichtNETCP.Inheritable;


namespace irrControl {
    public partial class irrControl : UserControl {


        private ISceneContainer currentScene = null;
        private ISceneContainer nextScene = null;
        
        
        public irrControl() {
            InitializeComponent();
        }

        private IrrlichtNETCP.Color back = new IrrlichtNETCP.Color( 0,0,0,0);

        private void irrControl_BackColorChanged(object sender, EventArgs e) {
            back.Set( BackColor.A,BackColor.R,BackColor.G,BackColor.B );
        }



        IrrlichtDevice device = null;
        private IrrlichtNETCP.DriverType driverType = DriverType.Direct3D9;  
        public IrrlichtNETCP.DriverType Driver_Type {
            get { return driverType; }
            set { driverType = value; }
        }


        bool runNow = false;
        bool resized = false;


        uint time = 0;
        protected override void  OnPaint(PaintEventArgs e){

            if (device == null && runNow) {
                device = new IrrlichtDevice(driverType,
                    new Dimension2D(Width, Height), 32, false, true, true, true, this.Handle);
                device.Resizeable = true;

                device.OnEvent += new OnEventDelegate(device_OnEvent); //We had a simple delegate that will handle every event
                resized = false;
                Invalidate();
            }
            else if (device != null && runNow) {
                if (device.Run() && runNow) {

                    if (nextScene != null) {
                        if (nextScene.setup(device)) {

                            if (currentScene != null) {
                                currentScene.dismiss();
                                currentScene = null;
                            }

                            currentScene = nextScene;
                            nextScene = null;
                            time = device.Timer.Time;
                        }
                    }

                    if (currentScene != null) {
                        if (resized) {
                            currentScene.refreshViewport(ClientSize.Width, ClientSize.Height);
                            resized = false;
                        }

                        VideoDriver driver = device.VideoDriver;

                        driver.BeginScene(true, true, currentScene.BackColor);
                        currentScene.render((device.Timer.Time- time)/1000.0f);
                        driver.EndScene();
                    }
                    time = device.Timer.Time;
                    Application.DoEvents();
                    Invalidate();
                }
                else {
                    if (currentScene != null) currentScene.dismiss();
                    device.Dispose();
                }
            }
            else {
                base.OnPaint(e);
            }

        }

        public void Run(){ runNow = true; }
        public void Stop() { runNow = false; }

        public bool device_OnEvent(Event p_event) {
            if (currentScene != null && currentScene.onEvent(p_event)) return true;
            else return false;
        }

        private void irrControl_Resize(object sender, EventArgs e) {
            if (device != null) {
                resized = true;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e) {            
        }

        public ISceneContainer RenderedScene {
            get { return currentScene; }
            set { nextScene = value; }
        }

        public float getFPS() { return device != null ? device.VideoDriver.FPS : 0; }
    }
}
